package com.leetcode.demo3Solution.JianOffer;

public class offer29 {
    /*剑指 Offer 29. 顺时针打印矩阵*/
    public static void main(String[] args) {
        Solution solution=new Solution();
        int[][]  arr={{1,2,3},{4,5,6},{7,8,9}};
        int[][]  arr1={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
        int[][]  arr2={{1,2,3}};
        for (int a :solution.spiralOrder(arr2)){
            System.out.println("顺时针打印:"+a);
        }
    }
}
class Solution {
    public int[] spiralOrder(int[][] matrix) {
        if(matrix.length>0){
            int left=0;
            int top=0;
            int right=matrix[0].length-1;
            int bottom=matrix.length-1,x=0;
            int[] res=new int[(right+1)*(bottom+1)];
            while (true){
                for(int i=left;i<=right;i++)     //从左到右
                {
                    res[x++]=matrix[top][i];
                }
                if(++top >bottom) break       ;

                for (int i=top;i<=bottom;i++){   //从上到下
                    res[x++]=matrix[i][right];
                }
                if(left>--right) break;

                for(int i=right;i>=left;i--){    //从右到左
                    res[x++]=matrix[bottom][i];
                }
                if(top>--bottom) break;

                for (int i=bottom;i>=top;i--)     //从下到上
                {
                    res[x++]=matrix[i][left];
                }
                if(++left>right) break;
            }
            return  res;
        }else
            return new int[0];
    }
}
